package com.jidong.test_springboot.web;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jidong.test_springboot.dao.RoleDao;
import com.jidong.test_springboot.dao.RoleRouterDao;
import com.jidong.test_springboot.dao.RouterDao;
import com.jidong.test_springboot.entity.Role;
import com.jidong.test_springboot.entity.RoleRouter;
import com.jidong.test_springboot.entity.Router;
import com.jidong.test_springboot.vo.ResultData;

@RestController
@RequestMapping("role/")
public class RoleServlet {
	@Autowired
	private RoleDao roleDao;
	@Autowired
	private RouterDao routerDao;
	@Autowired
	private RoleRouterDao roleRouterDao;
	
	@RequestMapping("list.do")
	public ResultData getAllRoleList(int current,int pageSize) {
		IPage page=new Page(current,pageSize);
		IPage<Role> record=roleDao.selectPage(page, null);
		long total=record.getTotal();
		List<Role> userRoles=record.getRecords();
		JSONObject result=new JSONObject();
		result.put("total", total);
		result.put("current",current);
		result.put("pageSize",pageSize);
		result.put("data", userRoles);
		return new ResultData().setCode(200).setResult(result);
	}
	
	@RequestMapping("add.do")
	public ResultData insertRole(Role role) {
		int result=roleDao.insert(role);
		if(result>0) {
			return new ResultData().setMsg("添加成功");
		}else {
			return new ResultData().setMsg("添加失败");
		}
		
	}
	
	@RequestMapping("update.do")
	public ResultData update(Role role) {
		int result=roleDao.updateById(role);
		if(result>0) {
			return new ResultData().setMsg("编辑成功");
		}else {
			return new ResultData().setMsg("编辑失败");
		}
		
	}
	
	@RequestMapping("delete.do")
	public ResultData delete(int id) {
		int result=roleDao.deleteById(id);
		if(result>0) {
			return new ResultData().setMsg("删除成功");
		}else {
			return new ResultData().setMsg("删除失败");
		}
		
	}
	
	/**获取所有的路由，便于用户授权使用，返回的数据将是一颗树*/
	@RequestMapping("getRoleRouterList.do")
	public ResultData getRoleRouterList() {
		List<Router> list=routerDao.selectList(null);
		JSONArray result=new JSONArray();
		this.getTree(result,list,null);
		
		return new ResultData().setCode(200).setResult(result);
	}
	
	
	
	private void getTree(JSONArray j_routers, List<Router> routers,Router parent) {
		for(Router d:routers) {
			JSONObject j_d=getDetp2Json(d);
			if(parent==null) {
				if("1".equals(d.getIsParent())) {
					JSONArray children=new JSONArray();
					j_d.put("children", children);
					getTree(children,routers,d);
				}
				
				if(d.getParentId()==0) {
					j_routers.add(j_d);
				}
			}else {
				if(parent.getId()==d.getParentId() ) {
					j_routers.add(j_d);
					if("1".equals(d.getIsParent())) {
						JSONArray children=new JSONArray();
						j_d.put("children", children);
						getTree(children,routers,d);
					}
				}
			}
		}
	}


	private JSONObject getDetp2Json(Router d) {
		JSONObject ob=new JSONObject();
		ob.put("title", d.getTitle());
		ob.put("key", d.getId()+"");
		ob.put("value", d.getId()+"");
		return ob;
	}
	
	@RequestMapping("getRoutersByRoleId.do")
	public ResultData getRoutersByRoleId(int id) {
		QueryWrapper<RoleRouter> query=new QueryWrapper<RoleRouter>();
		query.eq("role_id", id);
		List<RoleRouter> list=roleRouterDao.selectList(query);
		JSONArray result=new JSONArray();
		for(RoleRouter rr:list) {
			result.add(rr.getRouterId()+"");
		}
		return new ResultData().setCode(200).setResult(result);
	}
	
	@RequestMapping("updateRouter.do")
	public ResultData updateRouter(int roleid,int[] router_ids) {
		System.out.println(roleid);
		System.out.println(Arrays.toString(router_ids));
		//先删除之前的数据
		
		roleRouterDao.delete(new QueryWrapper<RoleRouter>().eq("role_id", roleid));
		//再插入新数据
		for(int router_id:router_ids) {
			RoleRouter r=new RoleRouter();
			r.setRoleId(roleid);
			r.setRouterId(router_id);
			roleRouterDao.insert(r);
		}
		return new ResultData().setMsg("授权成功");
		
		
	}
}
